Added ProtocolHelper and some tests
[brmtron.git] / BrmTron Server / src / cz / brmlab / brmtron / server / ProtocolHelper.java
blob03d681da685bce6bfbc18b142a139f6616c5107c
1 package cz.brmlab.brmtron.server;
3 import java.util.Map;
4 import java.util.Set;
6 public class ProtocolHelper {
8 //id --- name --- longitude --- latitude --- active\n
10 private static final int FIELD_ID = 0;
11 private static final int FIELD_NAME = 1;
12 private static final int FIELD_LONGITUDE = 2;
13 private static final int FIELD_LATITUDE = 3;
14 private static final int FIELD_ACTIVE = 4;
15 private static final String FIELD_DELIMITER = " --- ";
17 public static Player parsePlayer(String input) {
18 //TODO overeni vstupu
19 String[] tmp = input.trim().split(FIELD_DELIMITER);
20 int id = Integer.parseInt(tmp[FIELD_ID]);
21 Player ret = new Player(id);
22 ret.setName(tmp[FIELD_NAME]);
23 ret.setCoords(Double.parseDouble(tmp[FIELD_LONGITUDE]), Double.parseDouble(tmp[FIELD_LATITUDE]));
24 ret.setActive(Boolean.parseBoolean(tmp[FIELD_ACTIVE]));
25 return ret;
28 public static String buildPlayer(Player p) {
29 StringBuilder sb = new StringBuilder();
31 sb.append(p.getId());
32 sb.append(FIELD_DELIMITER);
33 sb.append(p.getName());
34 sb.append(FIELD_DELIMITER);
35 sb.append(p.getLongitude());
36 sb.append(FIELD_DELIMITER);
37 sb.append(p.getLatitude());
38 sb.append(FIELD_DELIMITER);
39 sb.append(p.isActive());
40 sb.append("\n");
42 return sb.toString();
46 /**
47 * @param players Numbered list of players
48 * @return String representation of each player's state
50 public static String getUpdate(Map<Integer, Player> players) {
51 StringBuilder sb = new StringBuilder();
52 Set<Integer> keys = players.keySet();
54 for(int i: keys) {
55 sb.append(buildPlayer(players.get(i)));
58 return sb.toString();